
#include "../Main.h"

#include "Tickbox.h"

#include "../Controller/Controller_Main.h"
#include "../Input.h"
#include "../TextRender.h"
#include "../TextureManager.h"

const u_int uTICKBOX_SIZE = 16;
const u_int uTICKBOX_COLOUR_HOVER = 0xDDDDDDFF;
const u_int uTICKBOX_COLOUR = 0xEEEEEEFF;
const u_int uTICKBOX_TEXTCOLOUR = 0x777777FF; 

Tickbox::Tickbox( Widget* pxParent ) : Panel( pxParent )
{
	m_szText[0] = 0;
	m_pbBool = 0;
	
	m_pxTickboxPanel = new Panel( this );
	m_pxTickboxPanel->SetSize( uTICKBOX_SIZE, uTICKBOX_SIZE );
	m_pxTickboxPanel->SetHasBorder( true );
	m_pxTickboxPanel->SetColour( uTICKBOX_COLOUR );
}

Tickbox::~Tickbox()
{

}
	
void Tickbox::Render()
{
	m_pxTickboxPanel->Render();

	if( m_szText[0] )
	{
		const u_int uMargin = 8;
		TextRender::SetColour( uTICKBOX_TEXTCOLOUR );
		TextRender::SetFont( FONT_SANS_12 );
		TextRender::Print( m_szText, GetResizedX() + uTICKBOX_SIZE + uMargin, GetResizedY() );
		
		u_int uTextWidth = 0;
		u_int uTextHeight = 0;
		TextRender::GetRenderSize( m_szText, uTextWidth, uTextHeight );
		
		Panel::SetSize( uTICKBOX_SIZE + uMargin + uTextWidth, uTICKBOX_SIZE );
	}

	if( m_pbBool && *m_pbBool )
	{
		TextureManager* pxTextureManager = Controller_Main::GetTextureManager();
		
		if( pxTextureManager )
		{
			const int iTextureID = pxTextureManager->GetTextureID( GetStringHash( "Tick32" ) );
			const u_int uMargin = 2;

			glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
			glBindTexture( GL_TEXTURE_2D, iTextureID );

			glBegin( GL_QUADS );

			glTexCoord2f( 0.0f, 0.0f );
			glVertex2i( GetResizedX() + uMargin, GetResizedY() + uMargin );
			glTexCoord2f( 0.0f, 1.0f );
			glVertex2i( GetResizedX() + uMargin, GetResizedY() + uTICKBOX_SIZE - uMargin );
			glTexCoord2f( 1.0f, 1.0f );
			glVertex2i( GetResizedX() + uTICKBOX_SIZE - uMargin, GetResizedY() + uTICKBOX_SIZE - uMargin );
			glTexCoord2f( 1.0f, 0.0f );
			glVertex2i( GetResizedX() + uTICKBOX_SIZE - uMargin, GetResizedY() + uMargin );

			glEnd();
		}
	}
}

void Tickbox::Update()
{
	if( MouseInWidget() )
	{
		m_pxTickboxPanel->SetColour( uTICKBOX_COLOUR_HOVER );

		if( m_pbBool )
		{
			Input* pxInput = Controller_Main::GetInputHandler();
			if( pxInput->MouseButtonPressed( INPUT_MOUSE_BUTTON_LEFT ) )
			{
				*m_pbBool = !(*m_pbBool);
				SetModified( true );
			}
		}
	}
	else
	{
		m_pxTickboxPanel->SetColour( uTICKBOX_COLOUR );
	}
}

void Tickbox::SetLabel( const char* szText )
{
	strncpy( m_szText, szText, 64 );
}

void Tickbox::SetBool( bool* pbBool )
{
	m_pbBool = pbBool;
}

// eof
